import { CoolController, BaseController } from '@cool-midway/core';
import { BroadcastPlayRecordEntity } from '../../entity/record';
import { UserInfoEntity } from '../../../user/entity/info';
import { Post, Inject, Body,Get } from '@midwayjs/decorator';
import { BaseSysConfService } from '../../../base/service/sys/conf';
import { BroadcastAudioEntity } from '../../entity/audio';

/**
 * 广播-播放记录
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: BroadcastPlayRecordEntity,
  // 分页查询配置
  pageQueryOp: {
    keyWordLikeFields: ["title"],
    fieldEq: ['userId', 'elephantNumber'],
    select: ['a.*', 'b.avatarUrl', 'b.nickName','c.name as audioName'],
    join: [
      {
        entity: UserInfoEntity,
        alias: 'b',
        condition: 'a.userId = b.id',
        type: 'leftJoin',
      },
      {
        entity: BroadcastAudioEntity,
        alias: 'c',
        condition: 'a.audioId = c.id',
        type: 'leftJoin',
      }
    ]
  },
  listQueryOp: {
    // 让title字段支持模糊查询
    keyWordLikeFields: ['elephantNumber'],
    fieldEq: ['userId', 'elephantNumber'],
    select: ['a.*', 'b.avatarUrl', 'b.nickName','c.name as audioName'],
    join: [
      {
        entity: UserInfoEntity,
        alias: 'b',
        condition: 'a.userId = b.id',
        type: 'leftJoin',
      },
      {
        entity: BroadcastAudioEntity,
        alias: 'c',
        condition: 'a.audioId = c.id',
        type: 'leftJoin',
      }
    ]
  },
})
export class AdminBroadcastPlayRecordsController extends BaseController {
  @Inject()
  baseSysConfService: BaseSysConfService;

  /**
  * 设置日志保存时间
  */
  @Post('/setKeep', { summary: '日志保存时间' })
  public async setKeep(@Body('value') value: number) {
    await this.baseSysConfService.updateVaule('recordKeep', value);
    return this.ok();
  }

  /**
  * 获得日志保存时间
  */
  @Get('/getKeep', { summary: '获得日志保存时间' })
  public async getKeep() {
    return this.ok(await this.baseSysConfService.getValue('recordKeep'));
  }
}
